package com.biye.api.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.base.common.edu.mapper.StaffMapper;
import com.base.common.edu.mapper.StudentMapper;
import com.base.common.httpStatusCode.enumDefine.StatusCode;
import com.biye.api.service.UserManageService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;

@Service
@Transactional
public class UserManageServiceImpl implements UserManageService {

    @Resource
    private StaffMapper staffMapper;

    @Resource
    private StudentMapper studentMapper;

    // 删除用户
    @Override
    public JSONObject delUser(Map<String, Object> requestMap) {
        JSONObject jsonObject = new JSONObject();
        String msg = "操作失败，请稍后再试";
        int statusCode = StatusCode.Failure._statusCode;
        // 判断请求参数的合法性
        if ( !requestMap.containsKey("userId") || !requestMap.containsKey("userType") ){
            jsonObject.put("msg", "请求参数不正确");
            jsonObject.put("statusCode", statusCode);
            return jsonObject;
        }
        // 获取请求参数
        String user_id = (String) requestMap.get("userId");
        user_id = "".equals(user_id) ? null : user_id;
        String user_type = (String) requestMap.get("userType");
        user_type = "".equals(user_type) ? null : user_type;
        if ( user_id == null || user_type == null ){
            jsonObject.put("msg", "请求参数不正确");
            jsonObject.put("statusCode", statusCode);
            return jsonObject;
        }
        // biz begin (进入业务线)
        if ( "Student".equals(user_type) ){
            int i = studentMapper.deleteStudent(user_id);
            if ( i > 0 ){
                msg = "操作成功！该学生账号已被系统删除";
                statusCode = StatusCode.Success._statusCode;
            }
        }else if( "Teacher".equals(user_type) ){
            int i = staffMapper.deleteStaff(user_id);
            if ( i > 0 ){
                msg = "操作成功！该职工账号已被系统删除";
                statusCode = StatusCode.Success._statusCode;
            }
        }
        jsonObject.put("msg", msg);
        jsonObject.put("statusCode", statusCode);
        return jsonObject;
    }

}
